package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(path = "/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable("subject") Integer subject){
        Course course=courseService.selectBySubject(subject);
        return Result.success(course);
    }

    @GetMapping("/list")
    public Result query(){
        List<Course> courseList =courseService.query("list");
        return Result.success(courseList);
    }
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程:{}", course);
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable ("id") Integer id){
        log.info("查询课程id:{}", id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程id:{}", id);
        courseService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("新增课程:{}", course);
        courseService.add(course);
        return Result.success();
    }
    @GetMapping
    public Result page(
            @RequestParam (name ="name", required = false)String name,
            @RequestParam(name = "subject", required = false)Integer subject,
            @RequestParam(name = "target", required = false)Integer target,
            @RequestParam(name = "page", defaultValue = "1")Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10")Integer pageSize){
            PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
            log.info("分页查询课程,参数:课程名称={},课程学科={},课程适用人群={},当前页码={},每页大小={}", name, subject, target, page, pageSize);
            return Result.success(pageResult);
    }
}
